package org.credo.jws.failure;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class SendSOAPToMMS {

	/**
	 * 以下代码演示了如何使用HttpURLConnection发送Soap请求调用webservice，并获取结果
	 * 
	 * @date 2011-12-9
	 * @param args
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public static void main(String[] args) {
		try {
			String userName = "testDJ";
			String password = "pbpb123456";
			String userNumbers = "18612033529";

			FileInputStream msgZipInputStream = new FileInputStream("D:\\restxml\\test2.zip");
			byte[] MMSContent = new byte[msgZipInputStream.available()];
			msgZipInputStream.read(MMSContent, 0, MMSContent.length);
			msgZipInputStream.close();
			// System.out.println("MMSContent:"+new String(MMSContent));
			int sendType = 1;
			String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body>" + "<SendMMS xmlns=\"Cn.Emay.MMSCenter\">" + "<userName>" + userName + "</userName>" + "<password>" + password + "</password>" + "<userNumbers>" + userNumbers + "</userNumbers>" + "<MMSContent>" + MMSContent + "</MMSContent>" + "<sendType>" + sendType + "</sendType>" + "</SendMMS>" + "</soap:Body>" + "</soap:Envelope>";
			// http://mmsplat.eucp.b2m.cn/mmscenterinterface/MMS.asmx?wsdl
			HttpURLConnection connection = (HttpURLConnection) new URL("http://mmsplat.eucp.b2m.cn/mmscenterinterface/MMS.asmx?wsdl").openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8");
			// connection.setRequestProperty("SOAPaction",
			// "Cn.Emay.MMSCenter/SendMMS");
			connection.setRequestProperty("Content-Length", String.valueOf(xml.getBytes().length));
			System.out.println("xml.getBytes().length:" + xml.getBytes().length);
			connection.connect();// 连接服务器
			OutputStream os = connection.getOutputStream();

			os.write(xml.getBytes());
			os.flush();
			os.close();

			System.out.println("here");
			System.out.println("1:" + connection.getResponseCode());
			System.out.println("1:" + connection.getResponseMessage());
			InputStream is = connection.getInputStream();
			byte[] bts = new byte[is.available()];
			System.out.println(new String(bts));
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
